package org.micode.common.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

/**
 * 分页类
 */
@Data
public class Page {

    /**
     * 默认的 每页记录数
     */
    @JsonIgnore
    public static final int DEFAULT_PAGE_SIZE = 20;

    /**
     * 页码
     */
    private Integer pageNo = 1;

    /**
     * 总记录数
     */
    private Long dataCount;

    /**
     * 总页数
     */
    private Integer pageCount;

    /**
     * 每页记录数
     */
    private Integer pageSize = DEFAULT_PAGE_SIZE;

    public static Page oneRecordPage() {
        Page page = new Page();
        page.pageSize = 1;
        return page;
    }

    public static Page defaultPage() {
        return new Page();
    }

    public static Page numberPage(int num) {
        Page page = new Page();
        page.pageSize = num;
        return page;
    }

    public void init(long dataCount) {
        this.dataCount = dataCount;
        this.pageCount = (int) Math.ceil(dataCount * 1.0d / pageSize);
    }

    public void init(int pageNo, long dataCount) {
        this.pageNo = pageNo;
        this.dataCount = dataCount;
        this.pageCount = (int) Math.ceil(dataCount * 1.0d / pageSize);
    }

    public void init(int pageNo, long dataCount, int pageSize) {
        this.pageNo = pageNo;
        this.dataCount = dataCount;
        this.pageSize = pageSize;
        this.pageCount = (int) Math.ceil(dataCount * 1.0d / pageSize);
    }

    @JsonIgnore
    public Integer getPageStart() {
        return (pageNo - 1) * pageSize;
    }

    @JsonIgnore
    public Integer getPageEnd() {
        return pageNo * pageSize;
    }

    @JsonIgnore
    public Integer getOffset() {
        return (pageNo - 1) * pageSize;
    }

    @JsonIgnore
    public boolean isFirstPage() {
        return null != pageNo && 1 >= pageNo;
    }

}
